<?php

declare(strict_types=1);

namespace App\Command;

use Hprose\Future;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Utils\Parallel;
use Hyperf\Utils\WaitGroup;
use Psr\Container\ContainerInterface;
use Swoole\Coroutine;

/**
 * @Command
 */
#[Command]
class HproseCommand extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('app:hprose');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('app:hprose');
    }

    /**
     * php74 bin/hyperf.php app:hprose
     * 异步阻塞
     * @return void
     */
    public function handle()
    {
        // 会创建一个主协程
        $result = \parallel([
           $this->func1()
        ]);
        echo "<pre>";
        print_r($result);
//        $parallel = new Parallel();
//        $parallel->add([
//            function () {
//                sleep(1);
//                return Coroutine::getCid();
//            },
//            function () {
//                sleep(1);
//                return Coroutine::getCid();
//            },
//        ]);
//        $results = $parallel->wait();
//        echo "<pre>";
//        print_r($results);


//        $wg = new WaitGroup();
//        $wg->add();
//        co(function () use ($wg) {
//            while(true) {
//                echo "TTT";
//                sleep(1);
//            }
//            $wg->done;
//        });
//        $wg->wait();
//        echo 'ZZZ';
    }

    public function func1()
    {
        return $_SERVER['REMOTE_ADDR'];
    }
}
